package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserShoppingCartMapper;
import com.sky.service.UserShoppingCartService;
import com.sky.vo.SetmealVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
@RequiredArgsConstructor
public class UserShoppingCartServiceImpl implements UserShoppingCartService {

    private final UserShoppingCartMapper userShoppingCartMapper;

    private final SetmealMapper setmealMapper;

    private final DishMapper dishMapper;

    @Override
    public List<ShoppingCart> list() {
        List<ShoppingCart> shoppingCartList = userShoppingCartMapper.selectShoppingCart(BaseContext.getCurrentId());
        return shoppingCartList;
    }

    @Override
    public void insertShoppingCart(ShoppingCart shoppingCart) {
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //添加前判断是否已经有该菜品或套餐
        ShoppingCart shoppingCartQuery = userShoppingCartMapper.selectById(shoppingCart);
        if (Objects.nonNull(shoppingCartQuery)) {
            //更新总金额
            BigDecimal amount = shoppingCartQuery.getAmount().divide(new BigDecimal(shoppingCartQuery.getNumber())).multiply(new BigDecimal(shoppingCartQuery.getNumber() + 1));
            shoppingCartQuery.setAmount(amount);
            //更新数量
            shoppingCartQuery.setNumber(shoppingCartQuery.getNumber() + 1);
            userShoppingCartMapper.updateById(shoppingCartQuery);
        } else {
            if (Objects.nonNull(shoppingCart.getDishId())) {
                Dish dish = dishMapper.selectById(shoppingCart.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setDishId(dish.getId());
                shoppingCart.setNumber(1);
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setCreateTime(LocalDateTime.now());
            }
            if (Objects.nonNull(shoppingCart.getSetmealId())) {
                SetmealVO setmealVO = setmealMapper.selectById(shoppingCart.getSetmealId());
                shoppingCart.setName(setmealVO.getName());
                shoppingCart.setSetmealId(setmealVO.getId());
                shoppingCart.setNumber(1);
                shoppingCart.setAmount(setmealVO.getPrice());
                shoppingCart.setImage(setmealVO.getImage());
                shoppingCart.setCreateTime(LocalDateTime.now());
            }
            userShoppingCartMapper.insertShoppingCart(shoppingCart);
        }
    }

    @Override
    public void deleteSole(ShoppingCart shoppingCart) {
        shoppingCart.setUserId(BaseContext.getCurrentId());
        ShoppingCart shoppingCartQuery = userShoppingCartMapper.selectById(shoppingCart);
        if (shoppingCartQuery.getNumber() == 1) {
            userShoppingCartMapper.deleteById(shoppingCart);
        } else {
            //更新总金额
            BigDecimal amount = shoppingCartQuery.getAmount().divide(new BigDecimal(shoppingCartQuery.getNumber())).multiply(new BigDecimal(shoppingCartQuery.getNumber() - 1));
            shoppingCartQuery.setAmount(amount);
            //更新数量
            shoppingCartQuery.setNumber(shoppingCartQuery.getNumber() - 1);
            userShoppingCartMapper.updateById(shoppingCartQuery);
        }
    }

    @Override
    public void cleanShoppingCart() {
        userShoppingCartMapper.cleanShoppingCart(BaseContext.getCurrentId());
    }
}
